//
//  classifDataBase.m
//  NewProject
//
//  Created by dlios on 15/3/9.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "classifDataBase.h"

@implementation classifDataBase

+(instancetype)shareClassifyDataBaseHandle
{
    static classifDataBase *classifyDBH = nil;
    
    if (classifyDBH == nil) {
        classifyDBH = [[classifDataBase alloc] init];
    }
    return classifyDBH;
}

static sqlite3 *classifyDB;


-(void)openDB
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentFielPath = [array lastObject];
    
    NSString *sqlitePath = [documentFielPath stringByAppendingPathComponent:@"class.sqlite"];
    
    if (classifyDB != nil) {
        return;
    }
    
    int result = sqlite3_open(sqlitePath.UTF8String, &classifyDB);
    
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }
}

// 创建表
-(void)createTable
{
    
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS myClass(number INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT,  img TEXT, name TEXT)";
    
    // 执行sql语句
    int result = sqlite3_exec(classifyDB, createSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    }
    
}

// 插入数据
-(void)insertMovie:(classifyModel *)classify
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO myClass(url, img, name)  VALUES('%@', '%@', '%@')", classify.url, classify.img, classify.name];
    
    //  执行sql语句
    int result = sqlite3_exec(classifyDB, insertSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }

}

// 删除一个数据
-(void)deleteClassify:(NSInteger)number
{
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM myClass WHERE number = '%ld'", (long)number];
    
    int result = sqlite3_exec(classifyDB, deleteSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }
    
}

// 查询所有数据
-(NSMutableArray *)selectClassify
{
    NSString *selectSql = @"SELECT * FROM myClass";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(classifyDB, selectSql.UTF8String, -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            int number = sqlite3_column_int(stmt, 0);
            const unsigned char *url = sqlite3_column_text(stmt, 1);
            const unsigned char *img = sqlite3_column_text(stmt, 2);
            const unsigned char *name = sqlite3_column_text(stmt, 3);
            
            NSInteger numberInteger = number;
            NSString *urlStr = [NSString stringWithUTF8String:(const char *)url];
            NSString *imgStr = [NSString stringWithUTF8String:(const char *)img];
            NSString *nameStr = [NSString stringWithUTF8String:(const char *)name];
            
            classifyModel *classify = [[classifyModel alloc] init];
            
            classify.url = urlStr;
            classify.img = imgStr;
            classify.name = nameStr;
            classify.number = numberInteger;
            [array addObject:classify];
        }
    
    }else
    {
        NSLog(@"查询准备失败");
    }

    return array;
    
}





@end
